package com.test.model;

import org.apache.hadoop.io.WritableComparable;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
 
public class xx implements WritableComparable<Object> {
    public String city;
    public String street;
    public int doorplate;
 
    public xx() {
        this("", "", 0);
    }
 
    public xx(String city, String street, int doorplate) {
        this.city = city;
        this.street = street;
        this.doorplate = doorplate;
    }
 
    @Override
    public void write(DataOutput out) throws IOException {
        out.writeUTF(this.city);
        out.writeUTF(this.street);
        out.writeInt(this.doorplate);
    }
 
    @Override
    public void readFields(DataInput in) throws IOException {
        this.city = in.readUTF();
        this.street = in.readUTF();
        this.doorplate = in.readInt();
    }
 
    @Override
    public int compareTo(Object o) {
        xx other = (xx) o;
        int n = this.strCompareTo(this.city, other.city);
        if (n != 0) return n;
        n = this.strCompareTo(this.street, other.street);
        if (n != 0) return n;
        return (this.doorplate < other.doorplate ? -1 : (this.doorplate == other.doorplate ? 0 : 1));
    }
 
    public String toString() {
        return this.city + "," + this.street + "," + this.doorplate;
    }
 
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof xx) {
            xx other = (xx) obj;
            return this.strEquals(this.city, other.city)
                    && this.strEquals(this.street, other.street)
                    && this.doorplate == other.doorplate;
        }
        return false;
    }
 
    /**
     * 重写 hashCode() 方法很重要，Hadoop 的 Partitioners 会用到这个方法
     */
    public int hashCode() {
        return 13 * (this.city == null ? 0 : this.city.hashCode())
                + 67 * (this.street == null ? 0 : this.street.hashCode())
                + 151 * this.doorplate;
    }
 
    public boolean strEquals(String a, String b) {
        return (a == null && b == null) || (a != null && a.equals(b));
    }
 
    public int strCompareTo(String a, String b) {
        if (a == null && b == null) return 0;
        else if (a != null && b == null) return 1;
        else if (a == null && b != null) return -1;
        else return a.compareTo(b);
    }
}
